/**  
* @Title: ShopTablesService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月2日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.mapper.cityRun.CrShopTablesMapper;
import com.lemon.mapper.cityRun.CrShopTablesServerMapper;
import com.lemon.model.cityRun.CrShopTables;
import com.lemon.model.cityRun.CrShopTablesServer;
import com.lemon.utils.QNFileUpload;
import com.lemon.utils.QrCodeUtils2;
import com.lemon.utils.StringUtil;
import com.lemon.utils.sval.StaticValues;

/**
 * <p>Title: ShopTablesService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月2日
 * @版本 V 1.0 
 */
@Service
public class ShopTablesServiceImpl implements ShopTablesService {

	@Resource
	private CrShopTablesServerMapper tablesServerMapper;
	@Resource
	private CrShopTablesMapper tabMapper;

	/**
	 * <p>
	 * Title: selectServerListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param server
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@Override
	public List<CrShopTablesServer> selectServerListBySelective(CrShopTablesServer server) throws Exception {
		PageHelper.startPage(server.getPageNum() , server.getPageSize());
		return tablesServerMapper.selectListBySelective(server);
	}

	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tab
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月2日
	 */
	@Override
	public List<CrShopTables> selectListBySelective(CrShopTables tab) throws Exception {
		PageHelper.startPage(tab.getPageNum() , tab.getPageSize());
		return tabMapper.selectListBySelective(tab);
	}

	/**
	 * <p>
	 * Title: updateBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tab
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@Override
	public int updateBySelective(CrShopTables tab) throws Exception {
		return tabMapper.updateByPrimaryKeySelective(tab);
	}

	/**
	 * <p>
	 * Title: insertBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param tab
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月2日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int insertBySelective(CrShopTables tab) throws Exception {
		int selective = tabMapper.insertSelective(tab);
		//返回主键之后封装到二维码中 然后修改二维码图片地址
		if (selective > 0) {
			//创建二维码
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
					.getRequest();
			String realPath = request.getSession().getServletContext().getRealPath("/");
			//将图片缓存到下面的文件下面
			File destFile = new File(realPath + StaticValues.UPLOAD_TEMP_PATH);
			//不存在则创建
			if (!destFile.exists()) {
				destFile.mkdirs();
			}
			//生成新的文件名称 //不含后缀
			String newFileName = StringUtil.getUUID();
			//生成二维码
			JSONObject js = new JSONObject();
			tab.setId(IdTypeHandler.encode(Long.parseLong(tab.getId())));
			js.put("id" , tab.getId());
			js.put("title" , tab.getTitle());
			newFileName = QrCodeUtils2.encode(js.toJSONString() , null , destFile.getAbsoluteFile() + "/" ,
					newFileName , true);
			//二维码的绝对路径
			String filepath = destFile.getAbsoluteFile() + "/" + newFileName;
			//上传到七牛存储空间
			String upload = QNFileUpload.upload(filepath , newFileName);
			tab.setQrcodePic(StaticValues.QINIUPATH + upload);
			//读取本地缓存二维码图片文件 , 并删除缓存文件
			File f = new File(filepath);
			f.delete();
			//将桌台二维码保存到数据库
			tabMapper.updateByPrimaryKeySelective(tab);

		}
		return selective;
	}
}
